# ES Archive Docker Makefile

.PHONY: help setup build start stop restart status logs shell clean test

# 默认目标
.DEFAULT_GOAL := help

# 颜色定义
BLUE := \033[36m
GREEN := \033[32m
YELLOW := \033[33m
RED := \033[31m
NC := \033[0m

# 帮助信息
help: ## 显示帮助信息
	@echo "$(BLUE)ES Archive Docker 管理命令$(NC)"
	@echo ""
	@echo "$(GREEN)基本命令:$(NC)"
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "  $(YELLOW)%-15s$(NC) %s\n", $$1, $$2}'
	@echo ""
	@echo "$(GREEN)示例:$(NC)"
	@echo "  make setup          # 初始化环境"
	@echo "  make start          # 启动所有服务"
	@echo "  make backup INDEX=my-index  # 备份指定索引"
	@echo "  make logs SERVICE=es-archive # 查看特定服务日志"

# 环境初始化
setup: ## 初始化 Docker 环境
	@echo "$(BLUE)初始化 Docker 环境...$(NC)"
	@./docker-run.sh setup

# 构建服务
build: ## 构建 Docker 镜像 (使用 MIRROR=镜像源 指定镜像源)
	@echo "$(BLUE)构建 Docker 镜像 (镜像源: $(or $(MIRROR),aliyun))...$(NC)"
	@./docker-run.sh build $(or $(MIRROR),aliyun)

# 构建所有镜像源版本
build-all: ## 构建所有镜像源版本
	@echo "$(BLUE)构建所有镜像源版本...$(NC)"
	@./build-docker.sh -a -c

# 启动服务
start: ## 启动所有服务
	@echo "$(BLUE)启动所有服务...$(NC)"
	@./docker-run.sh start

# 停止服务
stop: ## 停止所有服务
	@echo "$(BLUE)停止所有服务...$(NC)"
	@./docker-run.sh stop

# 重启服务
restart: stop start ## 重启所有服务

# 查看状态
status: ## 查看服务状态
	@./docker-run.sh status

# 查看日志
logs: ## 查看日志 (使用 SERVICE=服务名 指定特定服务)
	@./docker-run.sh logs $(SERVICE)

# 进入容器
shell: ## 进入 ES Archive 容器
	@./docker-run.sh shell

# 清理数据
clean: ## 清理所有数据和容器
	@echo "$(RED)警告: 这将删除所有数据!$(NC)"
	@./docker-run.sh clean

# 运行测试
test: ## 运行完整测试
	@echo "$(BLUE)运行 Docker 环境测试...$(NC)"
	@./test-docker.sh full

# 快速测试
test-quick: ## 运行快速测试（跳过初始化）
	@echo "$(BLUE)运行快速测试...$(NC)"
	@./test-docker.sh quick

# ES Archive 命令快捷方式
backup: ## 备份索引 (使用 INDEX=索引名 NAME=备份名)
	@if [ -z "$(INDEX)" ]; then \
		echo "$(RED)错误: 请指定索引名，例如: make backup INDEX=my-index$(NC)"; \
		exit 1; \
	fi
	@echo "$(BLUE)备份索引 $(INDEX)...$(NC)"
	@./docker-run.sh exec backup $(INDEX) /data/backups $(if $(NAME),--backup-name $(NAME),)

restore: ## 恢复数据 (使用 BACKUP_ID=备份ID TARGET=目标索引)
	@if [ -z "$(BACKUP_ID)" ] || [ -z "$(TARGET)" ]; then \
		echo "$(RED)错误: 请指定备份ID和目标索引，例如: make restore BACKUP_ID=backup_123 TARGET=my-index$(NC)"; \
		exit 1; \
	fi
	@echo "$(BLUE)恢复数据 $(BACKUP_ID) 到 $(TARGET)...$(NC)"
	@./docker-run.sh exec restore $(BACKUP_ID) $(TARGET)

list-backups: ## 列出所有备份
	@./docker-run.sh exec list-backups

analyze: ## 分析索引 (使用 INDEX=索引名)
	@if [ -z "$(INDEX)" ]; then \
		echo "$(RED)错误: 请指定索引名，例如: make analyze INDEX=my-index$(NC)"; \
		exit 1; \
	fi
	@echo "$(BLUE)分析索引 $(INDEX)...$(NC)"
	@./docker-run.sh exec analyze $(INDEX)

# 调试命令
debug-backup: ## 调试模式备份 (使用 INDEX=索引名)
	@if [ -z "$(INDEX)" ]; then \
		echo "$(RED)错误: 请指定索引名，例如: make debug-backup INDEX=my-index$(NC)"; \
		exit 1; \
	fi
	@echo "$(BLUE)调试模式备份索引 $(INDEX)...$(NC)"
	@./docker-run.sh exec --debug backup $(INDEX) /data/backups $(if $(NAME),--backup-name $(NAME),)

debug-restore: ## 调试模式恢复 (使用 BACKUP_ID=备份ID TARGET=目标索引)
	@if [ -z "$(BACKUP_ID)" ] || [ -z "$(TARGET)" ]; then \
		echo "$(RED)错误: 请指定备份ID和目标索引，例如: make debug-restore BACKUP_ID=backup_123 TARGET=my-index$(NC)"; \
		exit 1; \
	fi
	@echo "$(BLUE)调试模式恢复数据 $(BACKUP_ID) 到 $(TARGET)...$(NC)"
	@./docker-run.sh exec --debug restore $(BACKUP_ID) $(TARGET)

# 监控命令
health: ## 检查健康状态
	@echo "$(BLUE)检查服务健康状态...$(NC)"
	@echo "$(YELLOW)Elasticsearch:$(NC)"
	@curl -s http://localhost:9200/_cluster/health | jq '.' || echo "Elasticsearch 不可用"
	@echo "$(YELLOW)ES Archive 健康检查:$(NC)"
	@curl -s http://localhost:8081/health || echo "ES Archive 健康检查不可用"
	@echo "$(YELLOW)ES Archive 指标:$(NC)"
	@curl -s http://localhost:8080/metrics | head -10 || echo "ES Archive 指标不可用"

metrics: ## 查看指标
	@curl -s http://localhost:8080/metrics

# 镜像源快捷命令
build-aliyun: ## 使用阿里云镜像源构建
	@make build MIRROR=aliyun

build-tencent: ## 使用腾讯云镜像源构建
	@make build MIRROR=tencent

build-huawei: ## 使用华为云镜像源构建
	@make build MIRROR=huawei

build-ustc: ## 使用中科大镜像源构建
	@make build MIRROR=ustc

build-tsinghua: ## 使用清华大学镜像源构建
	@make build MIRROR=tsinghua

build-default: ## 使用官方镜像源构建
	@make build MIRROR=default

# 开发命令
dev-setup: setup build ## 开发环境完整设置
	@echo "$(GREEN)开发环境设置完成$(NC)"

dev-start: start ## 启动开发环境
	@echo "$(GREEN)开发环境已启动$(NC)"
	@echo "$(YELLOW)访问地址:$(NC)"
	@echo "  Elasticsearch: http://localhost:9200"
	@echo "  Grafana: http://localhost:3000 (admin/admin)"
	@echo "  Prometheus: http://localhost:9090"

# 生产环境命令
prod-build: ## 生产环境构建
	@echo "$(BLUE)构建生产环境镜像...$(NC)"
	@docker-compose -f docker-compose.yml -f docker-compose.prod.yml build

prod-start: ## 启动生产环境
	@echo "$(BLUE)启动生产环境...$(NC)"
	@docker-compose -f docker-compose.yml -f docker-compose.prod.yml up -d

# 维护命令
update: ## 更新镜像
	@echo "$(BLUE)更新 Docker 镜像...$(NC)"
	@docker-compose pull
	@docker-compose build --pull

backup-data: ## 备份 Docker 数据
	@echo "$(BLUE)备份 Docker 数据...$(NC)"
	@tar -czf es-archive-data-$(shell date +%Y%m%d-%H%M%S).tar.gz data/
	@echo "$(GREEN)数据备份完成$(NC)"

# 清理命令
clean-images: ## 清理未使用的镜像
	@echo "$(BLUE)清理未使用的 Docker 镜像...$(NC)"
	@docker image prune -f

clean-all: clean clean-images ## 完全清理（数据+镜像）

# 信息命令
info: ## 显示环境信息
	@echo "$(BLUE)Docker 环境信息:$(NC)"
	@echo "$(YELLOW)Docker 版本:$(NC)"
	@docker --version
	@echo "$(YELLOW)Docker Compose 版本:$(NC)"
	@docker-compose --version
	@echo "$(YELLOW)当前容器状态:$(NC)"
	@docker-compose ps
	@echo "$(YELLOW)磁盘使用情况:$(NC)"
	@du -sh data/ 2>/dev/null || echo "数据目录不存在"
